//
//  UILabel+Exs.swift
//  ttjx
//
//  Created by 文 on 2022/10/12.
//  Copyright © 2022 ttjx. All rights reserved.
//

import Foundation
import UIKit


extension UILabel{
    //计算label的行数
        func getRealLabelTextLines() -> Int {
            guard let labelText = text else {
                return 0
            }
            //计算理论上显示所有文字需要的尺寸
            let rect = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
            let labelTextSize = (labelText as NSString)
                .boundingRect(with: rect, options: .usesFontLeading,attributes: [NSAttributedString.Key.font: self.font!], context: nil)
            //计算理论上需要的行数
            let labelTextLines = Int(ceil(CGFloat(labelTextSize.height) / self.font.lineHeight))
            return labelTextLines
        }
}
